package com.wes.dsptest.info;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.wes.dsptest.R;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link InfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class InfoFragment extends Fragment {
    private static View rootView;
    private Context context;
    private static InfoFragment fragment;
    private static final String TAG = "InfoFragment";
    private static InfoPresenter infoPresenter;

    public InfoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment InfoFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static InfoFragment newInstance() {
        Log.i(TAG, "Info newInstance");
        fragment = new InfoFragment();
        return fragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        Log.i(TAG, "Info onAttach");
        if(infoPresenter == null)
            infoPresenter = new InfoPresenter(context);
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "Info onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (rootView == null)
            rootView = inflater.inflate(R.layout.fragment_info, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        LinearLayout scrollView = rootView.findViewById(R.id.ll_info);

        InfoView appVersionInfoView = new InfoView(context);
        appVersionInfoView.getTitleView().setText(R.string.version_name);
        appVersionInfoView.getContentView().setText(infoPresenter.getAppVersion());

        scrollView.addView(appVersionInfoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        InfoView deviceNameInfoView = new InfoView(context);
        deviceNameInfoView.getTitleView().setText(R.string.device_name);
        deviceNameInfoView.getContentView().setText(infoPresenter.getDeviceVersion());

        scrollView.addView(deviceNameInfoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        InfoView memInfoView = new InfoView(context);
        memInfoView.getTitleView().setText(R.string.mem_name);
        memInfoView.getContentView().setText(infoPresenter.getDeviceMem());

        scrollView.addView(memInfoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        InfoView runTimeInfoView = new InfoView(context);
        runTimeInfoView.getTitleView().setText(R.string.running_time);

        scrollView.addView(runTimeInfoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        //update time
        infoPresenter.startRunTime(runTimeInfoView);
    }

    @Override
    public void onPause() {
        Log.i(TAG, "Info onPause");
        infoPresenter.pause();
        super.onPause();
    }

    @Override
    public void onStop() {
        Log.i(TAG, "Info onStop");
        super.onStop();
    }

    @Override
    public void onResume() {
        Log.i(TAG, "Info onResume");
        infoPresenter.resume();
        super.onResume();
    }

    @Override
    public void onStart() {
        Log.i(TAG, "Info onStart");
        super.onStart();
    }

    @Override
    public void onDetach() {
        Log.i(TAG, "Info onDetach");
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "Info onDestroy");
        if(infoPresenter != null)
        {
            infoPresenter.close();
        }
        super.onDestroy();
    }

}